/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */
package model;

import java.util.Enumeration;
import javax.microedition.lcdui.List;
import model.Recipe;

/**

 *

 * @author k.suchomski

 */
public class SearchList implements Enumeration {

    private Link first;

    public SearchList() {

        first = null;

    }

    public boolean isEmpty() {

        return (first == null);

    }

    public void insert(Recipe key) {

        Link newLink = new Link(key);

        Link previous = null;

        Link current = first;



        while (current != null && key.getName().compareTo(current.data.getName()) > 0) {// >

            previous = current;

            current = current.next;

        }

        if (previous == null) {
            first = newLink;
        } else {
            previous.next = newLink;
        }

        newLink.next = current;

    }
    public Link getLast(){
        Link temp = first;
        Link prev=temp;
        while(temp!=null){
            prev=temp;
            temp=temp.next;
            
        }
        return prev;
    }

    public void addElement(Recipe recipe) {


        if ( recipe.getName() == null) {
           Link newLink = new Link(recipe);
           getLast().next=newLink;
           return;
        }

        Link newLink = new Link(recipe);

        Link previous = null;

        Link current = first;


        try {
            while (current != null && recipe.getName().compareTo(current.data.getName()) > 0) {// >

                previous = current;

                current = current.next;

            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("");

        }
        if (previous == null) {
            first = newLink;
        } else {
            previous.next = newLink;
        }

        newLink.next = current;

//        Link temp = first;
//        while(temp!=null){
//            System.out.println(temp.data.getName());
//            temp=temp.next;
//        }
    }

    public boolean hasNext(Link link) {
        return link.next != null;
    }

    public Link next(Link link) {
        return link.next;
    }

    public Recipe next(Recipe r5) {
        Link link = findLink(r5);
        if (link == null) {
            return null;
        }
        if (link.next == null) {
            return null;
        }
        return link.next.data;

    }

    public Recipe nextRecipe(Link link) {
        return link.data;
    }

    public Recipe findRecipe(Recipe recipe) {
        if (recipe == null) {
            return null;
        }
        Link temp = first;

        while (temp != null) {
            if (temp.data != null) {
                try {
                    if (temp.data.getName().compareTo(recipe.getName()) == 0) {
                        return temp.data;
                    }
                } catch (NullPointerException e) {
                    return null;
                }
            }
            temp = temp.next;
        }
        return null;
    }

    public Link findLink(Recipe recipe) {
        Link temp = first;

        while (temp != null) {
            if (temp.data.getName().compareTo(recipe.getName()) == 0) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    public boolean hasNext(Recipe recipe) {
        Link link = findLink(recipe);
        if (link == null) {
            return false;
        }

        return link.next != null;
    }

    public Recipe nextRecipe(Recipe recipe) {
        Link link = findLink(recipe);
        if (link != null) {
            return link.next.data;
        }
        return null;
    }

    public Link remove() {

        Link temp = first;

        if (first == null) {
            return null;
        }

        first = first.next;

        return temp;

    }

    public boolean remove(Recipe recipe) {
        if (remove(recipe.getName()) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public int remove(String searchedName) {
        if (first == null) {
            return 3;
        }

        if (first.data.getName().compareTo(searchedName) == 0) {
            first = first.next;
            return 0;
        }

        Link temp = first.next;
        Link prev = first;


        while (temp != null) {
            // System.out.println(temp.data.getName() + " " + temp.next.data.getName() +" = "+ String.valueOf(temp.data.getName().compareTo(temp.next.data.getName())));
            if (searchedName.compareTo(temp.data.getName()) == 0) {
                prev.next = temp.next;
                return 0;
            }
            temp = temp.next;
            prev = prev.next;
        }


        return 1;
    }

    //TODO
    public Object elementAt(int index) {
        Link temp = first;
        for (int i = 0; i < index; i++) {
            temp = temp.next;
        }
        return temp.data;
    }

    public void displayList() {

        System.out.print("List: ");

        Link current = first;

        while (current != null) {

            current.displayLink();

            current = current.next;

        }

        System.out.println("");

    }

    public int size() {
        Link temp = first;
        int size = 0;
        while (temp != null) {
            size++;
            temp = temp.next;
        }
        return size;


    }

    boolean contains(Recipe recipe) {
        Recipe foundRecipe = findRecipe(recipe);
        if (foundRecipe == null) {
            return false;
        } else {
            return true;

        }
    }

    boolean removeElement(Recipe recipe) {
        return remove(recipe);
    }

    public Object get(int index) {
        return elementAt(index);
    }

    public Recipe getFirst() {
        if (first == null) {
            return null;
        } else {
            return first.data;
        }
    }

    public void clear() {
        first = null;
    }

    public void add(Recipe recipe) {
        addElement(recipe);
    }
    private Link enumCurrent = first;
    private boolean enumMore =false;

    public boolean hasMoreElements() {
        if (enumCurrent == null) {
            return false;
        }
        if (enumCurrent.next == null) {
            return false;
        }
        if (enumCurrent.next.data == null) {
            return false;
        }
        return true;
    }

    public Object nextElement() {
        if (enumCurrent == null) {
            return null;
        }
        if (enumCurrent.next == null) {
            return null;
        }
        return enumCurrent.next.data;

    }

    public Enumeration elements() {
        enumCurrent = first;
        enumMore = false;
        return this;
    }

    /*  public static void mainn(String[] args) {

    SearchList theSortedList = new SearchList();

    Recipe r1 = new Recipe();

    r1.setName("aaa");

    theSortedList.insert(r1);



    Recipe r5 = new Recipe();

    r5.setName("zzz");

    theSortedList.insert(r5);



    theSortedList.displayList();



    Recipe r2 = new Recipe();

    r2.setName("fff");

    theSortedList.insert(r2);



    Recipe r3 = new Recipe();

    r3.setName("bbb");

    theSortedList.insert(r3);



    Recipe r4 = new Recipe();

    r4.setName("yyy");

    theSortedList.insert(r4);



    theSortedList.displayList();



    theSortedList.remove("aaa");
    //        theSortedList.remove();
    //        theSortedList.remove("a");

    boolean a = theSortedList.hasNext(r4);
    boolean b = theSortedList.hasNext(r1);


    Recipe c = theSortedList.next(r4);
    Recipe d = theSortedList.next(r5);
    try{
    System.out.println(r1.getName()+" 1 ->"+theSortedList.next(r1).getName());
    }catch(Exception e){

    }
    try{
    System.out.println(r2.getName()+" 1 ->"+theSortedList.next(r2).getName());
    }catch(Exception e){

    }
    try{
    System.out.println(r3.getName()+" 1 ->"+theSortedList.next(r3).getName());
    }catch(Exception e){

    }
    try{
    System.out.println(r4.getName()+" 1 ->"+theSortedList.next(r4).getName());
    }catch(Exception e){

    }

    try{
    System.out.println("yyy -> " +c.getName() );
    System.out.println("zzz -> " +d.getName());
    }catch(Exception e){

    }
    theSortedList.displayList();

    }
     */
    public class Link {

        public Recipe data;
        public Link next = null;

        public Link(Recipe dd) {

            data = dd;

        }

        public void displayLink() {

            System.out.print(data.getName() + " ");

        }
    }
}
